﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;
using MegaBitProject.Models;

namespace MegaBitProject.Controllers
{
    // Game hub for Pictunary

    public class GameHubPictionary : Hub
    {
        IGameRepository rep = new GameRepository();

        // When som1 join the hub
        public void Join(string groupID, string userName)
        {
            //Add new user to the group
            Groups.Add(Context.ConnectionId, groupID);
            // Tell other users that som1 has joined the hub
            Clients.OthersInGroup(groupID).userJoin(userName);
        }

        // Function to send "line" to other users in the group
        public void sendDraw(string groupID, string sessionId, string userName, string color, string brush, string lastX, string lasteY, string newX, string newY)
        {
            Clients.OthersInGroup(groupID).handleDraw(sessionId, color, brush, lastX, lasteY, newX, newY);
        }

        // Function that receive guss from users and check if it's the right word
        public void addGuess(string groupID, string sessionId, string userName, string guess, string word, string drawingUser)
        {
            Clients.OthersInGroup(groupID).newGuess(userName, guess);

            // If the guess is the right word
            if (guess == word)
            {
                // Get new random word
                int maxNum = (from max in rep.getWord
                              select max.ID).Max();

                Random rnd = new Random();
                string newWord = "";

                // New word can't be the same as the last one
                do
                {
                    int rand = rnd.Next(1, maxNum + 1);
                    newWord = (from w in rep.getWord
                               where w.ID == rand
                               select w.Word).SingleOrDefault().ToString();
                } while (newWord == word);

                int gameid = System.Int32.Parse(groupID);

                // Update db: set the new word and drawer 
                rep.updatePictionary(gameid, newWord, userName);
                
                UpdateScore(drawingUser);
                UpdateScore(userName);
                updateStats(drawingUser, "win");
                updateStats(userName, "win");

                // Update game: The winner is now drawer and all other have to guess
                Clients.Group(groupID).winner(sessionId, userName, newWord);
            }
        }

        // Function that receive message from user and send it to all other in the group
        public void addMessage(string groupID, string userName, string message)
        {
            Clients.OthersInGroup(groupID).newMessage(userName, message);
        }

        // Update users score by id
        public void UpdateScore(string id)
        {
            if (!(String.IsNullOrEmpty(id)))
            {
                rep.updateScore(id, 10);
            }
        }

        // Update users stats by id
        public void updateStats(string id, string result)
        {
            if (!(String.IsNullOrEmpty(id)))
            {
                Statistic s = new Statistic();

                s.result = result.ToLower();
                s.userID = (from user in rep.getUser
                            where user.UserName.ToLower() == id.ToLower()
                            select user).SingleOrDefault();

                rep.updateStats(s);
            }
        }
    }
}